package com.gm.englishdemo3.ui.user;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.gm.englishdemo3.R;
import com.gm.englishdemo3.adapter.HomeWorkAdapter;
import com.gm.englishdemo3.bean.HomeWorkBean;
import com.gm.englishdemo3.utils.Base64Util;
import com.gm.englishdemo3.utils.OkHttpTool;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.w3c.dom.Text;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class UserFragment extends Fragment {
    private Activity mActivityContext;

    private EditText name;

    private EditText studentId;

    private EditText projectId;

    private EditText year;

    private EditText pictrue;


    private Button save;

    private HomeWorkBean bean=new HomeWorkBean();


    @Override
    public void onAttach(@NonNull Context context) {
        this.mActivityContext=(Activity) context;
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.activity_user,container,false);
        initView(view);

        //设置控件事件
        setViewListener(view);
        return view;
    }

    private void setViewListener(View view) {
        pictrue.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                bean.setPhoto(pictrue.getText().toString());
            }
        });
        year.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                bean.setYear(Integer.parseInt(year.getText().toString()));
            }
        });
        name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                System.out.println("这里是输出"+name.getText().toString());
                bean.setTitle(name.getText().toString()+"");
            }
        });
        projectId.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                bean.setProjectId(Integer.parseInt(projectId.getText().toString()));
            }
        });
        studentId.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                bean.setStudentId(Integer.parseInt(studentId.getText().toString()));
            }
        });
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HomeWorkAdapter.insertData(bean);

            }
        });

    }

    private void initView(View view) {
        save=view.findViewById(R.id.save);
        pictrue=view.findViewById(R.id.pictrue);
        year=view.findViewById(R.id.year);
        projectId=view.findViewById(R.id.projectId);
        studentId=view.findViewById(R.id.studentId);
        name=view.findViewById(R.id.name);
    }
}
